package com.kingtone.rongji;

import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.kingtone.rongji.biz.CommonMessage;
import com.kingtone.rongji.network.UserRequest;
import com.kingtone.rongji.tool.AppContasts;
import com.kingtone.rongji.tool.SharedPreferencesUtil;
import com.kingtone.rongji.ui.ToastManager;

public class PasswordActivity extends BaseActivity {

	private static final String TAG = "PasswordActivity";

	private Activity mContext;
	
	private Button mSubmitBtn;

	private EditText mPassword;

	private EditText mNewPassword;
	
	private EditText mConfirmPassword;
	
	private boolean isResetPWD;
	
	private String token;
	
	private String userId;

	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		this.mContext = this;
		this.setContentView(R.layout.activity_change_pwd);
		setupView();
	}

	private void setupView() {
		
		initTitleView(getString(R.string.title_modify_pwd));
		
		userId = SharedPreferencesUtil.getInstance(mContext).getString(AppContasts.USERID_KEY);
		if(userId == null || userId.trim().length() == 0)
		{
			userId = this.getIntent().getStringExtra("UserId");
		}
		
		isResetPWD = this.getIntent().getBooleanExtra("ResetPassword", false);
		token = this.getIntent().getStringExtra("PublicToken");
		
		mPassword = (EditText) this.findViewById(R.id.id_et_oldPwd);
		mNewPassword = (EditText) this.findViewById(R.id.id_et_newPwd);
		mConfirmPassword = (EditText) this.findViewById(R.id.id_et_confirmPwd);

		if(isResetPWD)
		{
			mPassword.setVisibility(View.GONE);
		}
		else
		{
			mPassword.setVisibility(View.VISIBLE);
		}
		mSubmitBtn = (Button) this.findViewById(R.id.id_bt_submit);
		mSubmitBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String oldPwd = mPassword.getText().toString();
				String newPwd = mNewPassword.getText().toString();
				String confirmPwd = mConfirmPassword.getText().toString();
				if(isResetPWD)
				{
					oldPwd = newPwd;
				}
				if (oldPwd.equals("")) {
					ToastManager.show(mContext, getResources().getString(
							R.string.msg_old_password));
					return;
				} else if (newPwd.equals("")) {
					ToastManager.show(mContext, getResources().getString(
							R.string.msg_new_password));
					return;
				} 
				else if (confirmPwd.equals("")) {
					ToastManager.show(mContext, getResources().getString(
							R.string.msg_confirm_password));
					return;
				} 
				else if (!confirmPwd.equals(newPwd)) {
					ToastManager.show(mContext, getResources().getString(
							R.string.msg_password_not_equal));
					return;
				}
				changePassword(newPwd, oldPwd, token);
			}
		});
	}
	
	private void changePassword(String newPassword, String oldPassword, String token) {
		// TODO Auto-generated method stub
		UserRequest userRequest = new UserRequest();
		try {
			userRequest.changPwdStep1(mContext, userId, newPassword, oldPassword, token, this);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void onBussinessSuccess(Object obj) {
		super.onBussinessSuccess(obj);
		if(obj instanceof CommonMessage)
		{
			SharedPreferencesUtil shared = SharedPreferencesUtil.getInstance(mContext);
			shared.putBoolean(AppContasts.IS_LOGINED_KEY, false);
			Intent intent = new Intent(PasswordActivity.this, MainActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
		}
	}
}
